package com.bjtime.hprinter.Fragments.attrs

import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import com.bjtime.designer.utils
import com.bjtime.designer.widget.BarCodeView2
import com.bjtime.designer.widget.BaseLabelView
import com.bjtime.designer.widget.TextView2
import com.bjtime.hprinter.Dialogs.MIUIInputDialog
import com.bjtime.hprinter.R
import kotlinx.android.synthetic.main.viewpager_attrs_layout_base.*


class text  : Fragment , TextView2.TextView2AttrChangeListener {
    constructor()

    public interface TextView2Listener {

    }

    companion object {
        fun newInstance () : text {
            return text ()
        }
    }

    private var mView : View? = null
    private var text_bold       : CheckBox? = null
    private var text_italics    : CheckBox? = null
    private var text_underline  : CheckBox? = null
    private var text_deleteline : CheckBox? = null
    private var text_reverse : CheckBox? = null

    private var mTextView : TextView2? = null
    private var mShowTextView : TextView? = null
    private var mShowTextSize : TextView? = null
    private var mSizeAddBtn   : ImageButton? = null
    private var mSizeDecBtn   : ImageButton? = null

    private var attr_text : String = ""
    private var attr_size : Int = 0

    fun setView (v : BaseLabelView) {
        mTextView = v as TextView2
        mTextView!!.setChangeListener (this)

        text_bold!!.isChecked = mTextView!!.getBold()
        text_italics!!.isChecked = mTextView!!.getItalics()
        text_underline!!.isChecked = mTextView!!.getUnderline()
        text_deleteline!!.isChecked = mTextView!!.getDeleteline()
        text_reverse!!.isChecked = mTextView!!.getReverse()

        mShowTextView!!.setText(mTextView!!.getText())

        onTextChange (mTextView!!.getText())
        onSizeChange(utils.px2sp(context!!, mTextView!!.getTextSize().toFloat()))
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = LayoutInflater.from(context).inflate(R.layout.viewpager_attrs_layout_text, null)
        mView!!.findViewById<CheckBox>(R.id.attrs_base_expand).setOnCheckedChangeListener({ i, b->
            if (b) {
                mView!!.findViewById<View>(R.id.attrs_base_layout).visibility = View.VISIBLE
                text1.setText(resources.getString(R.string.bastattr_left))
                (text1.parent as LinearLayout).setBackgroundColor(Color.TRANSPARENT)
            } else {
                mView!!.findViewById<View>(R.id.attrs_base_layout).visibility = View.GONE
                text1.setText(resources.getString(R.string.bastattr_expand))
                (text1.parent as LinearLayout).setBackgroundColor(Color.WHITE)
            }
        })


        text_bold = mView!!.findViewById(R.id.tv_bold)
        text_italics = mView!!.findViewById(R.id.tv_italics)
        text_underline = mView!!.findViewById(R.id.tv_underline)
        text_deleteline = mView!!.findViewById(R.id.tv_deleteline)
        text_reverse = mView!!.findViewById(R.id.tv_reverse)

        text_reverse!!.setOnCheckedChangeListener { buttonView, isChecked ->
            mTextView!!.setReverse(isChecked)
        }

        text_bold!!.setOnCheckedChangeListener { buttonView, isChecked ->
            mTextView!!.setBold(isChecked)
        }

        text_italics!!.setOnCheckedChangeListener { buttonView, isChecked ->
            mTextView!!.setItalics(isChecked)
        }

        text_underline!!.setOnCheckedChangeListener { buttonView, isChecked ->
            mTextView!!.setUnderline(isChecked)
        }

        text_deleteline!!.setOnCheckedChangeListener { buttonView, isChecked ->
            mTextView!!.setDeleteline(isChecked)
        }

        if (mTextView != null) {
            text_bold!!.isChecked = mTextView!!.getBold()
            text_italics!!.isChecked = mTextView!!.getItalics()
            text_underline!!.isChecked = mTextView!!.getUnderline()
            text_deleteline!!.isChecked = mTextView!!.getDeleteline()
        }

        mSizeAddBtn = mView!!.findViewById(R.id.tv_textsize_add)
        mSizeDecBtn = mView!!.findViewById(R.id.tv_textsize_dec)
        mSizeAddBtn!!.setOnClickListener({i->
            mTextView?.setTextSize (utils.px2sp(context!!, mTextView?.getTextSize()!!.toFloat()) + 2)
        })

        mSizeDecBtn!!.setOnClickListener({i->
            mTextView?.setTextSize (utils.px2sp(context!!, mTextView?.getTextSize()!!.toFloat()) - 2)
        })

        mShowTextSize = mView!!.findViewById(R.id.tv_textsize)
        mShowTextView = mView!!.findViewById(R.id.tv_textview)
        mShowTextView!!.setOnClickListener({i->
            var dialog  = MIUIInputDialog(context!!)
            dialog.setTitle("条形码内容")
            dialog.show()
            dialog.setMaxLenght(80)
            dialog.setString (mShowTextView!!.text.toString())

            dialog.setOnButtonClickListener(object : MIUIInputDialog.OnButtonClick {
                override fun onOk(v: View) {
                    mShowTextView!!.setText(dialog.getString ())
                    if (mTextView != null)
                        mTextView!!.setText(mShowTextView!!.text.toString())
                }

                override fun onCancel(v: View) {

                }
            })
        })
        if (mTextView != null)
            mShowTextView!!.setText(mTextView!!.getText())

        mShowTextSize!!.setText(attr_size.toString())
        mShowTextView!!.setText(attr_text)

        return mView
    }


    override fun onTextChange(s: String) {
        attr_text = s
        if (mShowTextView != null)
            mShowTextView!!.setText(s)
    }

    override fun onSizeChange(size: Int) {
        attr_size = size
        if (mShowTextView != null)
            mShowTextSize!!.setText(attr_size.toString())
    }
}